<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="服务名称" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="服务类型" prop="serviceTypeName">
        <el-input
          v-model="queryParams.serviceTypeName"
          placeholder="请输入服务类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="服务状态" prop="statusName">
        <el-input
          v-model="queryParams.statusName"
          placeholder="请输入服务状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['manage:item:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:item:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:item:remove']"
          >删除</el-button
        >
      </el-col>
   
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="itemList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="服务编号" align="center" prop="serviceNumber" />
      <el-table-column label="服务名称" align="center" prop="serviceName" />
      <el-table-column
        label="服务图标
"
        align="center"
        prop="serviceIcon"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.serviceIcon"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="服务图片
"
        align="center"
        prop="servicePicture"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.servicePicture"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="服务价格
"
        align="center"
        prop="servicePrice"
      />
      <el-table-column
        label="服务单位
"
        align="center"
        prop="serviceUnit"
      />
      <el-table-column
        label="服务类型
"
        align="center"
        prop="serviceTypeName"
      />
      <el-table-column
        label="服务描述
"
        align="center"
        prop="serviceDescription"
      />
      <el-table-column
        label="排序
"
        align="center"
        prop="serviceSort"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          {{
            scope.row.status == 0
              ? "草稿"
              : scope.row.status == 1
              ? "已启用"
              : "已禁用"
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间
"
        align="center"
        prop="updateTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="服务详情
"
        align="center"
        prop="serviceDetails"
      />
      <el-table-column
        label="服务详情长图
"
        align="center"
        prop="serviceLengthChart"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview
            :src="scope.row.serviceLengthChart"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:item:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['manage:item:remove']"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="enabledStateApi(scope.row.id, scope.row.status)"
            >{{ scope.row.status == 1 ? "禁用" : "启动" }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务项管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
    
        <el-form-item label="服务名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item
          label="服务图标
"
          prop="serviceIcon"
        >
          <image-upload v-model="form.serviceIcon" />
        </el-form-item>
        <el-form-item
          label="服务图片
"
          prop="servicePicture"
        >
          <image-upload v-model="form.servicePicture" />
        </el-form-item>
        <el-form-item
          label="服务价格
"
          prop="servicePrice"
        >
          <el-input
            v-model="form.servicePrice"
            placeholder="请输入服务价格
"
          />
        </el-form-item>
        <el-form-item label="服务单位" prop="serviceUnit">
          <template>
            <el-select v-model="form.serviceUnit" placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.serviceUnit"
                :value="item.serviceUnit"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="服务类型" prop="serviceTypeId">
          <template>
            <el-select v-model="form.serviceTypeId" placeholder="请选择">
              <el-option
                v-for="item in typeList"
                :key="item.id"
                :label="item.serviceTypeName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item
          label="服务描述
"
          prop="serviceDescription"
        >
          <el-input
            v-model="form.serviceDescription"
            placeholder="请输入服务描述
"
          />
        </el-form-item>
        <el-form-item
          label="排序
"
          prop="serviceSort"
        >
          <el-input
            v-model="form.serviceSort"
            placeholder="请输入排序
"
          />
        </el-form-item>

        <el-form-item
          label="服务详情
"
          prop="serviceDetails"
        >
          <el-input
            v-model="form.serviceDetails"
            placeholder="请输入服务详情
"
          />
        </el-form-item>
        <el-form-item
          label="服务详情长图
"
          prop="serviceLengthChart"
        >
          <image-upload v-model="form.serviceLengthChart" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listItem,
  getItem,
  delItem,
  addItem,
  updateItem,
  enabledState,
  serviceTypeList,
} from "@/api/manage/item";

export default {
  name: "Item",
  data() {
    return {
      //单位
      options: [
        {
          value: "1",
          serviceUnit: "小时",
        },
        {
          value: "2",
          serviceUnit: "天",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务项管理表格数据
      itemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
       
        serviceNumber: null,
        serviceName: null,
        serviceIcon: null,
        servicePicture: null,
        servicePrice: null,
        serviceUnit: null,
        serviceTypeId: null,
        serviceDescription: null,
        serviceSort: null,
        status: null,
        updateTime: null,
        logicalDeletion: null,
        serviceDetails: null,
        serviceLengthChart: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      typeList: [],
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.serviceTypeListApp();
    
  },
  methods: {
    serviceTypeListApp() {
      serviceTypeList().then((response) => {
        this.typeList = response.data;
      });
    
    },

    /** 查询服务项管理列表 */
    getList() {
      this.loading = true;
       this.queryParams.serviceTypeId= this.$route.query.id
      listItem(this.queryParams).then((response) => {
        this.itemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serviceNumber: null,
        serviceName: null,
        serviceIcon: null,
        servicePicture: null,
        servicePrice: null,
        serviceUnit: null,
        serviceTypeId: null,
        serviceDescription: null,
        serviceSort: null,
        status: null,
        updateTime: null,
        logicalDeletion: null,
        serviceDetails: null,
        serviceLengthChart: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务项管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getItem(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务项管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateItem(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addItem(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      if(row.status!=0){
          this.$modal.msgError("该服务项管理已启用，无法删除");
      }else{
         const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除服务项管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delItem(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
      }
      
    },
    enabledStateApi(id, status) {
      if (status != 1) {
        status = 1;
      } else {
        status = 2;
      }
      /** 启用禁用 */
      enabledState(id, status).then((resp) => {
        this.getList();
        if (status == 1) {
          this.$modal.msgSuccess("启用成功");
        } else {
          this.$modal.msgSuccess("禁用成功");
        }
      });
    },
   
  },
};
</script>
